home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 13105 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.1 KB  |  98 lines

  1. Path: news-m01.ny.us.ibm.net!usenet
  2. From: rpsilva@ibm.net
  3. Newsgroups: comp.lang.c++
  4. Subject: Problems with Borland C++ 4.52  accessing global data within a DLL
  5. Date: 23 Mar 1996 16:02:14 GMT
  6. Message-ID: <4j17a6$2518@news-s01.ny.us.ibm.net>
  7. Reply-To: rpsilva@ibm.net
  8. NNTP-Posting-Host: slip166-72-221-84.tx.us.ibm.net
  9. X-Newsreader: IBM NewsReader/2 v1.2
  10.  
  11. Hi everybody,
  12.  
  13. I am porting some code from OS/2 to NT using Borland 4.52 compiler
  14. ( under OS/2 I am using Borland C++ 2.0 ).
  15.  
  16. I am facing a problem accessing static data members defined inside of a 
  17. DLL. The set of modules used to link the DLL itself, do not have any problem
  18. accessing them. The problem raises when an application uses the DLL and 
  19. accesses a static pointer( peharps with any global data ). I have written 
  20. a tiny example, and I've tested it with VC ++ 2.0 and it worked.
  21.  
  22.  
  23. Sample: ( Using Borland code ). It may have sintaxe errors
  24.  
  25. // DLL code
  26.  
  27. // MODULE mydll.h
  28. #ifdef __DLL__
  29. #define C_EXPORT   _export
  30. #else
  31. #define C_EXPORT
  32. #endif
  33.  
  34. class C_EXPORT MyClass
  35. {
  36. public:
  37.   MyClass();
  38.   ~MyClass();
  39.  
  40.   void method1( int x );      
  41.   static MyClass *ptr; 
  42.  
  43. private:
  44.   int y;
  45. };
  46. // End of mydll.h
  47. /////////////////////////////////////////////////////////
  48.  
  49. // MODULE mydll.cpp
  50. MyClass *MyClass::ptr = 0; 
  51.  
  52. MyClass::MyClass()
  53. {
  54.   if( !MyClass::ptr )
  55.      MyClass::ptr = this;
  56.   printf( "Ctor - MyClass: MyClass::ptr= %p\n",  MyClass::ptr );
  57.   y = 0;
  58. }
  59.  
  60. MyClass::~MyClass()
  61. {
  62.    printf( "Dtor - MyClass\n" );
  63. }
  64.  
  65. void MyClass::method1( int x )
  66. {
  67.    y = x;
  68.    printf( "method1: %d\n", x );
  69. }      
  70. // End of mydll.cpp
  71. /////////////////////////////////////////////////////////
  72.  
  73. // MODULE myexe.cpp
  74. #include <stdio.h>
  75. #include "mydll.h"
  76.  
  77. void main( void )
  78. {
  79.    MyClass m;
  80.  
  81.    m.method1( 3 );
  82.  
  83.    // Generates an access violation here
  84.    MyClass::ptr->method1( 4 );
  85. }
  86. // End of myexe.cpp
  87. /////////////////////////////////////////////////////////
  88.  
  89. Question:
  90.  
  91. 1. Is there a problem with the Borland C++ 4.52 accessing global data
  92. within a DLL under NT ? ( the same code works under OS/2 environment )
  93.  
  94. 2. Am I doing something wrong ?
  95.  
  96. Thanks for your help.
  97. Rosimildo.
  98.